﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AdmLaboratorios.DTO;
using AdmLaboratorios.Business;

namespace AdmLaboratorios
{
    public partial class frmGridCustomizer : Form
    {
        public frmGridCustomizer()
        {
            InitializeComponent();
        }

        internal void Inicializar(DataGridView grid)
        {
            btnGuardar.Enabled = true;

            foreach (DataGridViewColumn gvc in grid.Columns)
            {
                gv.Rows.Add(grid.FindForm().Name, grid.Name, gvc.Name, gvc.HeaderText, gvc.Visible);
            }
        }

        private void frmGridCustomizer_Load(object sender, EventArgs e)
        {

        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnGuardar_Click(object sender, EventArgs e)
        {
            try
            {
                List<GrillaColumnaDTO> columnas = new List<GrillaColumnaDTO>();
                foreach (DataGridViewRow gvr in gv.Rows)
                {
                    columnas.Add(new GrillaColumnaDTO()
                    {
                        Id = 0,
                        Formulario = (string)gvr.Cells["cFormulario"].Value,
                        Grilla = (string)gvr.Cells["cGrilla"].Value,
                        Columna = (string)gvr.Cells["cColumna"].Value,
                        Visible = (bool) gvr.Cells["cVisible"].Value
                    });
                }

                BusinessHelper.SetGrillaColumnas(columnas);
                FormsHelper.RefrescarColumnasGrilla();

                this.Close();
            }
            catch (Exception ex)
            {
                FormsHelper.MsgErrorInesperado(ex);
            }
        }
    }
}
